
PM_FUNC_NORM_ATTRIB ItemMenu( Int:iPlayer )
{
	if ( g_iState == PM_DISABLED )
		return;

	pokemod_funcstart_debug( "ItemMenu" );

	new Array:iPlayerItems = g_iPlayerItems[ iPlayer ];
	new Int:iSize = ArraySize( iPlayerItems ) / _:PLAYER_ITEM;
	if ( iSize )
	{
		new Int:iMenu = MenuCreate( "PokeItems" );

		for ( new Int:iItemID, Int:iItemAmount, Int:iItemNum, Int:iProp, String:szName[ NAME_LEN ], String:szData[ 16 ], String:szInfo[ 64 ];
			iItemNum < iSize;
			iItemNum++
		){
			iItemID = ArrayGetCell( iPlayerItems, ( ( iItemNum * _:PLAYER_ITEM ) + _:ITEM_ID ) );

			iProp = ArrayGetCell( g_iItemProps, iItemID );
			if ( iProp & ( ( 1 << _:ITEM_HIDDEN ) | ( 1 << _:ITEM_ARTIFACT ) ) )
				continue;

			ItemToName( iItemID, szName, charsmax(szName) );
			iItemAmount = ArrayGetCell( iPlayerItems, ( ( iItemNum * _:PLAYER_ITEM ) + _:ITEM_AMOUNT ) );
			ItemInfo( iItemID, "ItemInfo", szInfo, charsmax(szInfo) );

			formatex( szData, charsmax(szData), "u%d", iItemID );
			MenuAddItem( iMenu, szData, "%s (%d) - %s", szName, iItemAmount, szInfo );
		}

		MenuExit( iPlayer, iMenu, .bExitOption = true );
		MenuDisplay( iPlayer, iMenu, MENU_USE_ITEM );
	}
	else if ( g_iPlayerMenus[ iPlayer ] == Invalid_Array )
	{
		MenuMain( iPlayer, .bForceDisplay = true );
	}

	pokemod_funcend_debug( "ItemMenu" );
}

PM_FUNC_NORM_ATTRIB ItemUse( Int:iPlayer, Int:iItemID )
{
	new Int:iProp = ArrayGetCell( g_iItemProps, iItemID );
	if ( iProp & ( 1 << _:ITEM_NO_USE ) )
		return 0;

	pokemod_funcstart_debug( "ItemUse" );

	new String:szItemName[ NAME_LEN ];
	ItemToName( iItemID, szItemName, charsmax(szItemName) );

	new Int:iItemUsed;
	if ( g_iForwardUseItem > -1 && ( iProp & ( 1 << _:ITEM_CUSTOM ) ) )
	{
		ExecuteForward( g_iForwardUseItem, iItemUsed, iPlayer, iItemID, szItemName );
	}

	if ( !iItemUsed && ( iProp & ( 1 << _:ITEM_FISHING ) ) )
	{
		LocationScript( iPlayer, PlayerGetInfo( iPlayer, PINFO_LOCATION ), "Fishing", szItemName );

		iItemUsed = EncounterFight( iPlayer );
	}

	if ( !iItemUsed )
	{
		g_iScriptItem = iItemID;
		g_iScriptItemUsed = 0;

		FileScript( iPlayer, FileName( FILE_ITEMS ), szItemName, "ItemUse", "", ArrayGetCell( g_iItemTells, iItemID ) );

		g_iScriptItem = -1;

		iItemUsed = g_iScriptItemUsed;
		g_iScriptItemUsed = 0;
	}

	if ( !iItemUsed )
	{
		new Float:fCatchChance = str_to_float( ItemInfo( iItemID, "CatchChance" ) );
		if ( fCatchChance > 0.0 )
		{
			new Int:iEncounterID = PlayerGetInfo( iPlayer, PINFO_ENCOUNTER_POKEMON );
			new Int:iWiggles = PlayerGetInfo( iPlayer, PINFO_ENCOUNTER_WIGGLES );
			if ( iEncounterID > 0 && !iWiggles )
			{
				new Int:iEncounterHP = PlayerGetInfo( iPlayer, PINFO_ENCOUNTER_HP );
				new Int:iEncounterIVs = PlayerGetInfo( iPlayer, PINFO_ENCOUNTER_IVS );
				new Int:iEncounterLevel = PlayerGetInfo( iPlayer, PINFO_ENCOUNTER_LEVEL );
				new Int:iEncounterMaxHP = PokemonMaxHP( iEncounterID, iEncounterLevel, iEncounterIVs );
				if ( EncounterAttemptCatch( iPlayer, iEncounterID, iEncounterHP, iEncounterMaxHP, .iItemUsed = iItemID ) )
				{
					iItemUsed = 1;
				}
			}
		}
	}

	if ( iItemUsed )
	{
		if ( !( iProp & ( 1 << _:ITEM_UNLIMITED_USE ) ) )
		{
			PlayerGiveItem( iPlayer, iItemID, -1 );
		}
	}
	else
	{
		SoundDeny( iPlayer );
	}

	pokemod_funcend_debug( "ItemUse" );

	return iItemUsed;
}

PM_FUNC_NORM_ATTRIB ItemInfo( Int:iItemID, String:szInfo[], String:szCopy[] = "", Int:copyLen = 0 )
{
	pokemod_funcstart_debug( "ItemInfo" );

	new String:szData[ 64 ];

	nfv_get_data( FileName( FILE_ITEMS ), ItemToName( iItemID ), szInfo, szData, charsmax(szData), '#' );

	if ( copyLen )
		copy( szCopy, copyLen, szData );

	pokemod_funcend_debug( "ItemInfo" );

	return szData;
}

PM_FUNC_NORM_ATTRIB ItemEntityCount()
{
	pokemod_funcstart_debug( "ItemEntityCount" );

	new Int:iCount, Int:iEnt = -1;
	new Float:fGameTime = get_gametime();

	while ( ( iEnt = fm_find_ent_by_class( iEnt, szEntityPokeItem ) ) )
	{
		if ( EntityGetFloat( iEnt, ENTITY_KILLTIME ) <= fGameTime )
			EntityKill( iEnt );
		else
			iCount++;
	}

	pokemod_funcend_debug( "ItemEntityCount" );

	return iCount;
}

PM_FUNC_NORM_ATTRIB ItemEntityCreate()
{
	pokemod_funcstart_debug( "ItemEntityCreate" );

	new Int:iEnt = EntityCreate();

	set_pev( iEnt, pev_classname, szEntityPokeItem );

	EntitySetMoveType( iEnt, MOVETYPE_TOSS );
	EntitySetSolid( iEnt, SOLID_BBOX );
	EntitySetModel( iEnt, MODEL_POKEBALL );

	new Vector:vMins[3] = {-5.0,-5.0,-5.0};
	new Vector:vMaxs[3] = {5.0,5.0,5.0};
	EntitySetSize( iEnt, vMins, vMaxs );

	new Vector:vMinOrigin[3], Vector:vMaxOrigin[3];
	new Vector:vOrigin[3], Int:iMaxEnts = global_get( glb_maxEntities );

	new Int:i, Int:j;
	for( i = 0; i < iMaxEnts; i++ )
	{
		if( EntityValid( i ) )
		{
			EntityGetOrigin( i, vOrigin );
			if ( vOrigin[0] == 0.0 && vOrigin[1] == 0.0 && vOrigin[1] == 0.0 )
				get_brush_entity_origin( i, vOrigin );

			for( j = 0; j < 3; j++ )
			{
				if( vOrigin[ j ] > vMaxOrigin[ j ] )
					vMaxOrigin[ j ] = vOrigin[ j ];
				if( vOrigin[ j ] < vMinOrigin[ j ] )
					vMinOrigin[ j ] = vOrigin[ j ];
			}
		}
	}

	if ( get_distance_f( vMinOrigin, vMaxOrigin ) <= 500.0 )
	{
		for ( i = 0; i < 3; i++ )
		{
			vMinOrigin[ i ] -= 100.0;
			vMaxOrigin[ i ] += 100.0;
		}
	}

	for ( i = 0; i < 3; i++ )
	{
		vOrigin[ i ] = random_float( vMinOrigin[ i ], vMaxOrigin[ i ] );
	}
	EntitySetOrigin( iEnt, vOrigin );
	if ( !UnStuck( iEnt, .iHullsize = HULL_HEAD, .iAttempts = 128 ) )
	{
		EntitySetFloat( iEnt, ENTITY_KILLTIME, get_gametime() + 1.0 );

		pokemod_funcend_debug( "ItemEntityCreate Bad Origin" );

		return -1;
	}

	EntitySetSize( iEnt, vMins, vMaxs );

	new Vector:vAngles[3];
	for ( i = 0; i < 3; i++ )
		vAngles[ i ] = random_float( -360.0, 360.0 );

	EntitySetAngles( iEnt, vAngles );

	EntitySetFloat( iEnt, ENTITY_KILLTIME, get_gametime() + 300.0 );

	pokemod_funcend_debug( "ItemEntityCreate" );

	return iEnt;
}

PM_FUNC_NORM_ATTRIB ItemHelp( Int:iPlayer, Int:iItemID )
{
	pokemod_funcstart_debug( "ItemHelp" );

	new String:szMOTD[ 1024 ], Int:iLen;

	new String:szName[ 64 ];
	ItemToName( iItemID, szName, charsmax(szName) );

	new Int:iProp = ArrayGetCell( g_iItemProps, iItemID );
	if ( iProp & ( 1 << _:ITEM_ARTIFACT ) )
		add( szName, charsmax(szName), " (Artifact)" );

	iLen += formatex( szMOTD[ iLen ], charsmax(szMOTD) - iLen, "<center><b>%s</b><br><br>%s</center>", szName, ItemInfo( iItemID, "ItemInfo" ) );

	show_motd( iPlayer, szMOTD, szName );

	pokemod_funcend_debug( "ItemHelp" );
}




